define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                  
                  data: {
                    list:[],
                    shop_info:{},
                    type:0
                  },
                  loading: 0,
                  frisload:1,
                  table_id:0,
                  tableInfo:{},
                  use_yie:0,
                  disabled:0,
                  shop_id:0,
                  card_code:'',
                  order_amount:0,
                  user_info: {},
                  card_title:'',
                };
            },
            el: '#pageCheckout',
            created: function() {
                this.fetchData();
            },
             mounted: function() {
                    
                    if (this.mounted == 1) {
                        return;
                    }
                    // this.bindPick();
                    var that = this ;
                   this.mounted = 1;
            },


            methods: {
                fetchData: function() {
                    var _this = this;
                    this.table_id=this.$route.params.table_id;
                    this.getInfo();
                    this.getTableInfo();
                    
                },
              //桌位信息
              getTableInfo: function () {
                var that = this;
                app.get("mall/mall_tables/find", {
                  id: this.table_id
                }, function(res){
                  if (res.code != 200) {
                    app.alert(res.msg);
                  }
                  that.shop_id = res.data.shop_id;
                  that.setData({ tableInfo: res.data });
                  // that.getShopInfo();
                           
                });
              },
              // 用户信息
                getInfo: function () {
                  var that = this;
                  app.get("api/user/info", {
                    fields: "avatarUrl,user_id,last_update_time,nickname"
                  }, function (res) {

                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                 
                    that.setData({user_info: res.data });
               
                  });
                },
          
              //打开商家所在地点
              openlocation:function(){
                 var that = this;
                  app.wx(function(wx) {
                      wx.openLocation({
                        latitude: Number(that.data.data.shop_info.latitude),
                        longitude: Number(that.data.data.shop_info.longitude),
                        scale: 28,
                        address: that.data.data.shop_info.fulladdress,
                        name: that.data.data.shop_info.name,
                      });
                });
              },       
              setData:function(arr){
                  for( var i in arr){
               
                     this[i] = arr[i];
                  }
                },
              //加载购物车
                loadCart:function(reload) {
                  var that = this;
                  if (reload == 1) {
                    this.setData({
                      loading: 0,
                      listData: [],
                    });
                  }
                  this.loading = 1;
                  app.get("mall/cart/getcartByUserId", {
                    table_id: this.table_id,
                    card_code: this.card_code,
                  }, function (res) {
                    that.loading = 0;
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    that.frisload = 0;
                    // 卡券不可用
                    if (res.data.cardRes && res.data.cardRes.code>0){
                      app.alert(res.data.cardRes.msg);
                      that.card_code="";
                      that.card_title="";
                    }
                    that.data = res.data;
                    that.shop_info = res.data.shop_info;
                  });
                },
                  /**
                 * 生命周期函数--监听页面显示
                 */
                pageShow: function () {
                  
                  this.loadCart();

                },
                suc:function(){
                 
                },
                //生成订单
                formSubmit:function(e){                  
                  var that = this;
                  that.setData({ loading: 1, disabled: 1 });
                  that.payloading = 1;
                  var remark = $("#pageCheckout").find("[name='remark']").val();
                  app.post("api/pay/bookOffline",{
                    remark: remark?remark:'',
                    shop_id: this.shop_id,
                    // paytype:'offline_payment',
                    card_code: this.card_code,
                    table_id: this.table_id,
                  },function(res){
                    if (res.code != 200) {
                      app.alert(res.msg);
                      that.payloading = 0;
                      that.setData({ 'loading': 0 });
                      that.setData({ 'disabled': 0 });
                      return false;
                    }
                    var order_sn = res.data.order_sn;
                    if(res.data.paytype!="auto"){
                       app.pay(order_sn,res.data.paytype);
                     }else{
                       app.pay(order_sn);
                     }
                   
                  });
                },
                moreAct:function(e){
                  var that = this;
                  var itemListArray = [];
                  var goods_id = e.currentTarget.dataset.goods_id;
                  var goods_spec_id = e.currentTarget.dataset.goods_spec_id;
                  
                },
                //选择卡券
                chooseCard:function(){
                  var that = this;
                  app.component("card",{
                     success:function(res){
                        that.$router.go(-1);
                        that.card_code = res.card_code;
                        that.card_title = res.card_title;
                        that.loadCart(1);
                     }
                  });
                }


            }

        }).$mount('#app');
    };
    module.exports = controller;
});
